import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;


public class Starter {

	
	public static void main(String[] args) throws IOException{
		ExcelReader reader = new ExcelReader();
		HashMap<String, ArrayList<EANObject>> newItemList = checkData(reader);
		//HashMap<String, ArrayList<EANObject>> newItemList = new HashMap<String, ArrayList<EANObject>>();
		checkNewItems(newItemList, reader);
		
		//rewriteFormula();
	}
	
	private static void writeNewListFiles(ExcelReader reader, ArrayList<String> priceChangedList) {
		try {
			HashMap<String, ArrayList<EANObject>> mainList = reader.readXLSX("Complete assortiment ONVERANDERD.xlsx");
			reader.writeNewListFiles(priceChangedList,mainList);
			
		} catch (IOException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private static void checkNewItems(HashMap<String, ArrayList<EANObject>> newItemList , ExcelReader reader) throws IOException {
		try {
			HashMap<String, ArrayList<EANObject>> existingList = reader.readXLSX("newItemsList.xlsx");
			PrintWriter writer = new PrintWriter("excel nieuwe artikelen logFile.txt", "UTF-8");
			
			for(ArrayList<EANObject> list : newItemList.values()){
				for(EANObject newEANObject: list){
					if(existingList.get(newEANObject.getEan()) == null || existingList.get(newEANObject.getEan()).size() == 0){
						//artikel has not been found previously
						writer.println("Artikel : " + newEANObject.getEan() + " is nieuw gevonden");
						
						ArrayList<EANObject> addList = existingList.get(newEANObject.getEan());
						if(addList == null){
							addList = new ArrayList<EANObject>();
						}
						addList.add(newEANObject);
						existingList.put(newEANObject.getEan(), addList);
					}
				}
			}
			writer.close();
			
			reader.createNewListFile(existingList,"newItemsList.xlsx");
		} catch (IOException e) {
			reader.createNewListFile(newItemList,"newItemsList.xlsx");
			PrintWriter writer = new PrintWriter("excel nieuwe artikelen logFile.txt", "UTF-8");
			for(ArrayList<EANObject> list : newItemList.values()){
				for(EANObject newEANObject: list){
					writer.println("Artikel : " + newEANObject.getEan() + " is nieuw gevonden");
				}
			}
			writer.close();
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	public static HashMap<String, ArrayList<EANObject>> checkData(ExcelReader reader){
		try {
			HashMap<String, ArrayList<EANObject>> supplierList = reader.readXLSX("checklist 11-11-2013.xlsx");
			HashMap<String, ArrayList<EANObject>> mainList = reader.readXLSX("Complete assortiment ONVERANDERD.xlsx");
			
			HashMap<String, ArrayList<EANObject>> newItemList = new HashMap<String, ArrayList<EANObject>>(); 
			
			PrintWriter writer = new PrintWriter("excel comparing logFile.txt", "UTF-8");
			
			ArrayList<String> listToRemove = new ArrayList<String>();
			ArrayList<String> priceChangedList = new ArrayList<String>();
			
			for(ArrayList<EANObject> list : mainList.values()){
				for(EANObject mainListEANObject: list){
					if(mainListEANObject.getEan() != null){
						ArrayList<EANObject> supplierCheckList = supplierList.get(mainListEANObject.getEan());
						if(supplierCheckList != null && !supplierCheckList.isEmpty()){							
							EANObject lowest = null;
							for(EANObject supplierObject : supplierCheckList){
								if(lowest != null){
									if(lowest.getPrice() > supplierObject.getPrice()){
										lowest = supplierObject;
									}
								}
								else{
									lowest = supplierObject;
								}
								
							}
							
							if(mainListEANObject.isInStock() != lowest.isInStock()){
								writer.println("de stock van de hoofdlijst (" + mainListEANObject.getEan() + " : " + mainListEANObject.isInStock() + ") komt niet overeen met leverancier (" + lowest.getEan() + " : " + lowest.isInStock() + ")");
								mainListEANObject.setInStock(lowest.isInStock());
								listToRemove.add(mainListEANObject.getEan());
							}
							
							if(mainListEANObject.getPrice() != lowest.getPrice()){
								writer.println("de prijs van de hoofdlijst (" + mainListEANObject.getEan() + " : " + mainListEANObject.getPrice() + ") was hoger dan die van leverancier (" + lowest.getEan() + " : " + lowest.getPrice() + ") Prijs is aangepast");
								mainListEANObject.setPrice(lowest.getPrice());
								if(lowest.getSupplier() != null){
									mainListEANObject.setSupplier(lowest.getSupplier());
								}
								priceChangedList.add(mainListEANObject.getEan());
							}
							
						}
						else{
							writer.println("de ean van de hoofdlijst (" + mainListEANObject.getEan() + ") bestaat niet in de leveranciers lijst");
							listToRemove.add(mainListEANObject.getEan());
						}
					}
					else{
						ArrayList<EANObject> supplierCheckList = supplierList.get(mainListEANObject.getEan());
						if(supplierCheckList != null && !supplierCheckList.isEmpty()){
							for(EANObject supplierObject: supplierCheckList){
								if(mainListEANObject.getArticleNumber().equals(supplierObject.getArticleNumber())){
									if(mainListEANObject.isInStock() != supplierObject.isInStock()){
										writer.println("de stock van de hoofdlijst (Artikelnummer " + mainListEANObject.getArticleNumber() + " : " + mainListEANObject.isInStock() + ") komt niet overeen met leverancier " + supplierObject.getSupplier() + " (" + supplierObject.getArticleNumber() + " : " + supplierObject.isInStock() + ")");
										mainListEANObject.setInStock(supplierObject.isInStock());
									}
									
									if(mainListEANObject.getPrice() != supplierObject.getPrice()){
										writer.println("de prijs van de hoofdlijst (" + mainListEANObject.getEan() + " : " + mainListEANObject.getPrice() + ") was hoger dan die van leverancier (" + supplierObject.getEan() + " : " + supplierObject.getPrice() + ") Prijs is aangepast");
										mainListEANObject.setPrice(supplierObject.getPrice());
										mainListEANObject.setSupplier(supplierObject.getSupplier());
										priceChangedList.add(mainListEANObject.getEan());
									}
								}
							}
						}
						else{
							writer.println("de ean van de hoofdlijst (" + mainListEANObject.getEan() + ") bestaat niet in de leveranciers lijst en is verwijderd");
						}
					}
				}
			}
			
			HashMap<String, ArrayList<EANObject>> deletedList = new HashMap<String, ArrayList<EANObject>>();
			for(String key: listToRemove){
				deletedList.put(key, mainList.get(key));
				
				mainList.remove(key);
			}
			reader.createNewListFile(deletedList, "removedItems.xlsx");
			
			reader.createNewListFile(mainList, "Complete assortiment ONVERANDERD.xlsx");
			
			if(!priceChangedList.isEmpty()){
				writeNewListFiles(reader, priceChangedList);
			}
			
			
			for(ArrayList<EANObject> list : supplierList.values()){
				for(EANObject mainListEANObject: list){
					
					ArrayList<EANObject> supplierCheckList = mainList.get(mainListEANObject.getEan());
					if(supplierCheckList == null || supplierCheckList.size() == 0){
						//bestaat nog niet in hoofdlijst
						if(supplierCheckList == null){
							supplierCheckList = new ArrayList<EANObject>();
						}
						supplierCheckList.add(mainListEANObject);
						newItemList.put(mainListEANObject.getEan(), supplierCheckList);
					}
				}
			}
			
			writer.close();
			return newItemList;
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	//Temporary method
	public static void rewriteFormula(){
		ExcelReader reader = new ExcelReader();
		try {
			HashMap<String, ArrayList<EANObject>> mainList = reader.readXLSX("Complete assortiment ONVERANDERD.xlsx");
			
			int counter = 2;
			double number = 6.75;
			for(ArrayList<EANObject> list : mainList.values()){
				for(EANObject mainListEANObject: list){
					mainListEANObject.setAskingPriceFormula("SUM(L" + counter + "-"+number+")");
					counter++;
				}
			}
			
			reader.createNewListFile(mainList, "Complete assortiment ONVERANDERD.xlsx");
		} catch (IOException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
